#pragma once

extern CWindow Window;
extern CGl     gl;

class CRender
{
public:
	CRender()
	{
		PIXELFORMATDESCRIPTOR pfd;
		memset(&pfd,0,sizeof(pfd));

		pfd.nSize        = sizeof(pfd);
		pfd.nVersion     = 1;
		pfd.dwFlags      = PFD_DOUBLEBUFFER|PFD_DRAW_TO_WINDOW|PFD_SUPPORT_OPENGL;
		pfd.cColorBits   = 32;
		pfd.cDepthBits   = 24;
		pfd.cStencilBits = 8;

		m_hdc = GetDC(Window.GetHandle());

		int cpf =ChoosePixelFormat(hdc,&pfd);

		ASSERT(cpf,"Unsupported pixel format");

		SetPixelFormat(hdc,cpf,&pfd);
		hglrc = wglCreateContext(hdc);

		ASSERT(cpf,"Unable create OGL context");

		wglMakeCurrent(hdc,hglrc);
		SetViewport(0,0,800,600);
		SetClearColor(color::red());

		gl.Init();
	}
	~CRender()
	{
		wglMakeCurrent(0,0);
		wglDeleteContext(hglrc);
		ReleaseDC((HWND)Window.GetHandle(),(HDC)hdc);
	}
	void Clear(bool _color,bool _depth)
	{
		u32 flags = 0;
		if(_color)
			flags |= GL_COLOR_BUFFER_BIT;
		if(_depth)
			flags |= GL_DEPTH_BUFFER_BIT;
		glClear(flags);
	}
	void Swap()
	{
		SwapBuffers((HDC)hdc);
	}
	void SetViewport(u32 _x,u32 _y,u32 _width,u32 _height)
	{
		m_viewport = rect(_x,_y,_x+_width,_y+_height);
		glViewport(_x,_y,_width,_height);
	}
	rect& GetViewport()
	{
		return m_viewport;
	}
	void SetClearColor(const color& _color)
	{
		m_clcolor = _color;
		glClearColor(_color.r/255.0f,_color.g/255.0f,_color.b/255.0f,_color.a/255.0f);
	}

private:
	HDC     m_hdc;
	HGLRC   m_hglrc;

	rect  m_viewport;
	color m_clcolor;
};